package sk.vrto.web.views;

import com.vaadin.ui.*;
import sk.vrto.web.presenters.NewEmailPresenter;
import sk.vrto.web.ui.ImageDropBox;

public class NewEmailView extends VerticalLayout {

    transient private NewEmailPresenter presenter;

    // ui
    private ComboBox contactsBox;
    private ContactViewPopup triggeredFrom;
    protected TextField title;
    protected RichTextArea mailArea;

    public NewEmailView(NewEmailPresenter presenter) {
        this.presenter = presenter;
        presenter.setView(this);

        setSpacing(true);
        setWidth("99%");
        setHeight("95%");

        addComponent(suggestableContactsBar());
        addComponent(titleArea());
        addComponent(attachmentsBar());

        mailArea = mailArea();
        addComponent(mailArea);

        Component sendButton = sendButton();
        addComponent(sendButton);
        setComponentAlignment(sendButton, Alignment.MIDDLE_RIGHT);
        setExpandRatio(mailArea, 1.0f);
    }

    public void mailSent(String address) {
        getWindow().showNotification("Your mail has been send to " + address);
    }

    public void insertContact(String email) {
        contactsBox.addItem(email);
    }

    public void deleteContact(String email) {
        contactsBox.removeItem(email);
    }

    private Component titleArea() {
        title = new TextField();
        title.setWidth("99%");
        title.setInputPrompt("Title");
        title.setStyleName("left-marged");
        return title;
    }

    private Component sendButton() {
        Button send = new Button("Send message");
        send.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                presenter.sendMail(
                        contactsBox.getValue().toString(),
                        title.getValue().toString(),
                        mailArea.getValue().toString());

                // clear stuff
                String address = contactsBox.getValue().toString();
                contactsBox.setValue(null);
                title.setValue("");
                mailArea.setValue("");

                mailSent(address);
            }
        });
        return send;
    }

    private RichTextArea mailArea() {
        mailArea = new RichTextArea();
        mailArea.setWidth("99%");
        mailArea.setHeight("100%");
        mailArea.setStyleName("left-marged");
        return mailArea;
    }

    private Component attachmentsBar() {
        ProgressIndicator progress = new ProgressIndicator();
        progress.setIndeterminate(true);
        progress.setVisible(false);
        addComponent(progress);

        HorizontalLayout dropPane = new HorizontalLayout();
        dropPane.setHeight("40px");
        dropPane.setWidth("600px");

        ImageDropBox dropBox = new ImageDropBox(dropPane, progress);
        dropBox.setSizeUndefined();

        Panel panel = new Panel(dropBox);
        panel.setDescription("Drag content here");
        panel.setCaption("Attachments (drag 'n' drop content)");
        panel.setStyleName("left-marged");
        panel.setWidth("99%");

        return panel;
    }

    private Component suggestableContactsBar() {
        contactsBox = new ComboBox("", presenter.getAddresses());
        contactsBox.addStyleName("left-marged");
        contactsBox.setWidth("99.4%");
        contactsBox.setImmediate(true);
        contactsBox.setInputPrompt("Start typing recepient ...");
        contactsBox.setNewItemsAllowed(true);
        contactsBox.setFilteringMode(AbstractSelect.Filtering.FILTERINGMODE_CONTAINS);

        return contactsBox;
    }

    public ComboBox getContactsBox() {
        return contactsBox;
    }

    public void setTriggeredFrom(ContactViewPopup triggeredFrom) {
        this.triggeredFrom = triggeredFrom;
    }

    public ContactViewPopup getTriggeredFrom() {
        return triggeredFrom;
    }

    public TextField getTitle() {
        return title;
    }

    public RichTextArea getMailArea() {
        return mailArea;
    }
}
